package ar.com.angrymachine.hunter.activities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.Game;
import ar.com.angrymachine.hunter.GraphicStyles;
import ar.com.angrymachine.hunter.Locale;
import ar.com.angrymachine.hunter.HunterResourceManager;
import ar.com.angrymachine.hunter.gameplay.Trap;

public class TrapDoorActivity extends Activity
{
    public int timeToFire = 0;
    private Trap trap;
    private Activity behind;
    
    public TrapDoorActivity(Activity behind)
    {
        this.trap = new Trap("Trapped Door", HunterResourceManager.getImage("door-trapped.png"), 50, 1);
        this.behind = behind;
    }
    
    public String getDescription()
    {
        return trap.name;
    }
    
    public String getActionText()
    {
        if(isCompleted())
        {
            if(trap.isFired())
            {
                return Locale.TRIGGERED;
            }
            else
            {
                return Locale.DISARMED;
            }
        }
        else
        {
            if(isRunning())
            {
                return Locale.DISARMING;
            }
            else
            {
                return Locale.DISARM;
            }
        }
    }
    
    public boolean isCompleted()
    {
        return trap.isFired() || trap.isDisarmed();
    }

    public void update(int delta, boolean action, boolean timeIsRunning)
    {
        if (timeIsRunning)
        {
            if (action)
            {
            	trap.wake();
                trap.disarmTime += Game.current.player.getDisarmPower() * delta;
                if (trap.isDisarmed())
                {
                	end(true);
                    return;
                }
            }
            trap.tick(delta);
            if (trap.isFired())
            {
            	end(true);
            }
        }
    }
    
    public void paint(Graphics g, Rectangle activityArea)
    {
        GraphicStyles.paintDefaultActivity(g, this, activityArea, trap.icon);
        if(!isCompleted())
        {
        	Rectangle[] bars = GraphicStyles.getBarAreas(activityArea, 2);
            Image springIcon = HunterResourceManager.getImage("trap-spring.png");
            Image disarmIcon = HunterResourceManager.getImage("trap-disarm.png");
        	int iconsWidth = Math.max(springIcon.getWidth(null), disarmIcon.getWidth(null));
        	// spring
            g.setColor(Color.YELLOW);
            GraphicStyles.paintIconizedBar(g, springIcon, iconsWidth, bars[0], trap.fireTime, trap.timeToFire);
            // disarm
            g.setColor(Color.BLUE);
            GraphicStyles.paintIconizedBar(g, disarmIcon, iconsWidth, bars[1],  trap.disarmTime, trap.timeToDisarm);
        }
    }

    public void onFinished()
    {
    	Game.current.currentLevel.addActivity(this, behind);    
    }
}
